class Solution {
public:
    int numSquares(int n) {
        int m = sqrt(n);
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        for (int j = 1; j <= n; j++) dp[0][j] = 0x3f3f3f3f;
        for (int i = 1; i <= m; i++)
            for (int j = 0; j <= n; j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= i * i)
                    dp[i][j] = min(dp[i][j], dp[i][j - i * i] + 1);
            }
        return dp[m][n];

    }
};
class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        int len = strs.size();
        vector<vector<vector<int>>> dp(len + 1, vector<vector<int>>(m + 1, vector<int>(n + 1)));
        for (int i = 1; i <= len; i++)
        {
            int a = 0, b = 0;
            for (auto ch : strs[i - 1])
            {
                if (ch == '0') a++;
                else b++;
                for (int j = 0; j <= m; j++)
                    for (int k = 0; k <= n; k++)
                    {
                        dp[i][j][k] = dp[i - 1][j][k];
                        if (j >= a && k >= b)
                            dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - a][k - b] + 1);
                    }
            }
        }
        return dp[len][m][n];

    }
};
